package com.tlgen.controller;

import com.tlgen.config.Refresh;
import com.tlgen.config.YmlPropertySourceFactory;
import com.tlgen.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Objects;

import static com.tlgen.constant.Constant.READ_FILE_PATH;

@Refresh
@RestController
@RequestMapping("/speed/cooperos")
public class CooperosController {

    @Autowired
    private ConfigurableEnvironment environment;

    @PostMapping("/publish")
    public String publish(@RequestParam("content") String content) {
        FileUtils.createFile(content);
        PropertySource<?> propertySource = YmlPropertySourceFactory.loadProfiles();
        if(Objects.nonNull(propertySource)) {
            environment.getPropertySources().addLast(propertySource);
        }
        return "SUCCESS";
    }

    @GetMapping("/readFile")
    public String readFile() {
        return FileUtils.readFile(new File(READ_FILE_PATH));
    }

}
